package applet.admin.components.attendees;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.util.ArrayList;

import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.BevelBorder;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;

import data.CMSRes;
import data.account.attendee.AttendeeAccount;
import data.account.attendee.lodging.LodgingData;
import data.account.attendee.travel.TravelData;
import data.event.Event;
import data.event.EventTable;

public class AttendeeDialog extends JDialog
{
	private static final long serialVersionUID = 880843906232638857L;
	
	public AttendeeDialog(AttendeeAccount attendee)
	{
		super(CMSRes.frame(), "Attendee Overview", true);
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		
		JPanel panel = new JPanel(new BorderLayout());
		panel.add(getPersonalInfoPanel(attendee), BorderLayout.NORTH);
		
		if(attendee.travel() != null)
		{
			JPanel layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.add(panel, BorderLayout.NORTH);
			layoutHelper.add(getTravelInfoPanel(attendee.travel()), BorderLayout.SOUTH);
			panel = layoutHelper;
		}
		
		if(attendee.lodging() != null)
		{
			JPanel layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.add(panel, BorderLayout.NORTH);
			layoutHelper.add(getLodgingInfoPanel(attendee.lodging()), BorderLayout.SOUTH);
			panel = layoutHelper;
		}
		
		JPanel layoutHelper = new JPanel(new BorderLayout());
		layoutHelper.add(panel, BorderLayout.NORTH);
		layoutHelper.add(getSchedulePanel(attendee), BorderLayout.SOUTH);
		panel = layoutHelper;
		
		panel.setBorder(new CompoundBorder(new BevelBorder(BevelBorder.LOWERED), new EmptyBorder(5, 5, 5, 5)));
		setLayout(new BorderLayout());
		add(new JScrollPane(panel), BorderLayout.CENTER);
		pack();
		if(getHeight() > 400)
			setSize(getWidth() + 30, 400);
		setResizable(false);
	}
	
	private JPanel getPersonalInfoPanel(AttendeeAccount attendee)
	{
		JPanel panel = new JPanel(new GridLayout(0, 2));
		panel.setBorder(new CompoundBorder(new EmptyBorder(2, 2, 2, 2), new LineBorder(Color.LIGHT_GRAY, 1)));
		
			JPanel layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(10, 10, 10, 2));
				JLabel labelHelper = new JLabel("Personal Information:");
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
			layoutHelper.add(labelHelper, BorderLayout.WEST);
		panel.add(layoutHelper);
		panel.add(new JPanel());
		
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(10, 10, 10, 2));
				labelHelper = new JLabel("Name:");
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
			layoutHelper.add(labelHelper, BorderLayout.EAST);
		panel.add(layoutHelper);
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setOpaque(false);
			layoutHelper.setBorder(new EmptyBorder(10, 2, 10, 10));
				labelHelper = new JLabel(attendee.fullName());
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
			layoutHelper.add(labelHelper, BorderLayout.WEST);
		panel.add(layoutHelper);
		
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(10, 10, 10, 2));
				labelHelper = new JLabel("Organization:");
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
			layoutHelper.add(labelHelper, BorderLayout.EAST);
		panel.add(layoutHelper);
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setOpaque(false);
			layoutHelper.setBorder(new EmptyBorder(10, 2, 10, 10));
				labelHelper = new JLabel(attendee.org());
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
			layoutHelper.add(labelHelper, BorderLayout.WEST);
		panel.add(layoutHelper);
		return panel;
	}
	
	private JPanel getLodgingInfoPanel(LodgingData lodging)
	{
		JPanel panel = new JPanel(new GridLayout(0, 2));
		panel.setBorder(new CompoundBorder(new EmptyBorder(2, 2, 2, 2), new LineBorder(Color.LIGHT_GRAY, 1)));

			JPanel layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(10, 10, 10, 2));
				JLabel labelHelper = new JLabel("Lodging Information:");
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
			layoutHelper.add(labelHelper, BorderLayout.WEST);
		panel.add(layoutHelper);
		panel.add(new JPanel());
		
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(10, 10, 10, 2));
				labelHelper = new JLabel("Hotel:");
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
			layoutHelper.add(labelHelper, BorderLayout.EAST);
		panel.add(layoutHelper);
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setOpaque(false);
			layoutHelper.setBorder(new EmptyBorder(10, 2, 10, 10));
				labelHelper = new JLabel("Holiday Inn");
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
			layoutHelper.add(labelHelper, BorderLayout.WEST);
		panel.add(layoutHelper);
		
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(10, 10, 10, 2));
				labelHelper = new JLabel("Floor:");
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
			layoutHelper.add(labelHelper, BorderLayout.EAST);
		panel.add(layoutHelper);
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setOpaque(false);
			layoutHelper.setBorder(new EmptyBorder(10, 2, 10, 10));
				labelHelper = new JLabel("" + lodging.floor());
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
			layoutHelper.add(labelHelper, BorderLayout.WEST);
		panel.add(layoutHelper);

			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(10, 10, 10, 2));
				labelHelper = new JLabel("Room:");
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
			layoutHelper.add(labelHelper, BorderLayout.EAST);
		panel.add(layoutHelper);
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setOpaque(false);
			layoutHelper.setBorder(new EmptyBorder(10, 2, 10, 10));
				labelHelper = new JLabel("Suite " + lodging.room());
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
			layoutHelper.add(labelHelper, BorderLayout.WEST);
		panel.add(layoutHelper);
	
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(10, 10, 10, 2));
				labelHelper = new JLabel("Arrival Date:");
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
			layoutHelper.add(labelHelper, BorderLayout.EAST);
		panel.add(layoutHelper);
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setOpaque(false);
			layoutHelper.setBorder(new EmptyBorder(10, 2, 10, 10));
				labelHelper = new JLabel(lodging.startDate());
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
			layoutHelper.add(labelHelper, BorderLayout.WEST);
		panel.add(layoutHelper);
		
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(10, 10, 10, 2));
				labelHelper = new JLabel("Duration of Stay:");
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
			layoutHelper.add(labelHelper, BorderLayout.EAST);
		panel.add(layoutHelper);
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setOpaque(false);
			layoutHelper.setBorder(new EmptyBorder(10, 2, 10, 10));
				labelHelper = new JLabel("" + lodging.duration() + " days");
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
			layoutHelper.add(labelHelper, BorderLayout.WEST);
		panel.add(layoutHelper);
		
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(10, 10, 10, 2));
				labelHelper = new JLabel("Meal Plan:");
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
			layoutHelper.add(labelHelper, BorderLayout.EAST);
		panel.add(layoutHelper);
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setOpaque(false);
			layoutHelper.setBorder(new EmptyBorder(10, 2, 10, 10));
				labelHelper = new JLabel(lodging.mealPlan());
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
			layoutHelper.add(labelHelper, BorderLayout.WEST);
		panel.add(layoutHelper);
	
		return panel;
	}
	
	private JPanel getTravelInfoPanel(TravelData travel)
	{
		JPanel panel = new JPanel(new GridLayout(0, 2));
		panel.setBorder(new CompoundBorder(new EmptyBorder(2, 2, 2, 2), new LineBorder(Color.LIGHT_GRAY, 1)));

			JPanel layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(10, 10, 10, 2));
				JLabel labelHelper = new JLabel("Travel Information:");
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
			layoutHelper.add(labelHelper, BorderLayout.WEST);
		panel.add(layoutHelper);
		panel.add(new JPanel());
		
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(10, 10, 10, 2));
				labelHelper = new JLabel("Airline:");
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
			layoutHelper.add(labelHelper, BorderLayout.EAST);
		panel.add(layoutHelper);
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setOpaque(false);
			layoutHelper.setBorder(new EmptyBorder(10, 2, 10, 10));
				labelHelper = new JLabel(travel.airline());
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
			layoutHelper.add(labelHelper, BorderLayout.WEST);
		panel.add(layoutHelper);
		
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(10, 10, 10, 2));
				labelHelper = new JLabel("Arrival Date:");
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
			layoutHelper.add(labelHelper, BorderLayout.EAST);
		panel.add(layoutHelper);
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setOpaque(false);
			layoutHelper.setBorder(new EmptyBorder(10, 2, 10, 10));
				labelHelper = new JLabel(travel.arrivalDate());
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
			layoutHelper.add(labelHelper, BorderLayout.WEST);
		panel.add(layoutHelper);
		
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(10, 10, 10, 2));
				labelHelper = new JLabel("Arrival Time:");
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
			layoutHelper.add(labelHelper, BorderLayout.EAST);
		panel.add(layoutHelper);
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setOpaque(false);
			layoutHelper.setBorder(new EmptyBorder(10, 2, 10, 10));
				labelHelper = new JLabel(travel.arrivalTime());
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
			layoutHelper.add(labelHelper, BorderLayout.WEST);
		panel.add(layoutHelper);
	
		return panel;
	}
	
	private JPanel getSchedulePanel(AttendeeAccount attendee)
	{
		JPanel panel = new JPanel(new BorderLayout());
		panel.setBorder(new CompoundBorder(new EmptyBorder(2, 2, 2, 2), new LineBorder(Color.LIGHT_GRAY, 1)));
		
			JPanel layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(10, 10, 10, 10));
				JLabel labelHelper = new JLabel("Schedule:");
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
			layoutHelper.add(labelHelper, BorderLayout.WEST);
		
		panel.add(layoutHelper, BorderLayout.NORTH);
		
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(10, 10, 10, 10));
				
				ArrayList<Event> scheduledEvents = new ArrayList<Event>();
				for(Event event : CMSRes.conference().events())
					if(event.attendees().contains(attendee.userID()))
						scheduledEvents.add(event);
				JPanel layout2 = new JPanel(new BorderLayout());
				layout2.setBorder(new BevelBorder(BevelBorder.LOWERED));
				EventTable eventTable = new EventTable(scheduledEvents);
				layout2.add(eventTable.getTableHeader(), BorderLayout.NORTH);
				layout2.add(eventTable, BorderLayout.CENTER);
				
			layoutHelper.add(layout2, BorderLayout.SOUTH);
			
		panel.add(layoutHelper, BorderLayout.CENTER);
		
		return panel;
	}
}